package cn.huhst.cas.weisiqian.pojo;

import cn.huhst.cas.weisiqian.util.JSON;

public class User {

	public User() {

	}

	public User(String username, String userpassword, int role) {
		this.username = username;
		this.userpassword = userpassword;
		this.role = role;
	}

	// 用户名
	private String username;
	// 用户密码
	private String userpassword;
	// 用户角色
	private int role;

	/**
	 * 用户名由2到5个汉字组成或者由4到10个词字符组成（词字符：大小写字母和数字）
	 */
	public static final String usernameRegx = "^([\\u4e00-\\u9fa5]{2,5})|([a-zA-Z0-9]{4,10})$";

	/**
	 * 密码不少于6个词字符但不多于22个词字符(词字符：大小写字母和数字)
	 */
	public static final String userpasswordRegx = "^\\w{6,22}$";

	/**
	 * 用户角色只能为正整数 1：表示会员 2：表示管理员
	 */
	public static final String roleRegx = "^[1-9]+\\d*$";

	public static JSON checkUserNameFormat(String username, JSON json) {
		if (username == null || !username.matches(usernameRegx)) {
			json.put("username", "用户名由2到5个汉字组成或者由4到10个词字符组成（词字符：大小写字母和数字）");
		}
		return json;
	}

	public static JSON checkUserPasswordFormat(String userpassword, JSON json) {
		if (userpassword == null || !userpassword.matches(userpasswordRegx)) {
			json.put("userpassword", "密码不少于6个词字符但不多于22个词字符(词字符：大小写字母和数字)");
		}
		return json;
	}

	public static JSON checkRoleFormat(String role, JSON json) {
		if (role == null || !role.matches(roleRegx)) {
			json.put("role", "用户角色只能为正整数");
		}
		return json;
	}

	public JSON checkFormat() {
		JSON json = new JSON();
		json = checkUserNameFormat(username, json);
		json = checkUserPasswordFormat(userpassword, json);
		return json;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUserpassword() {
		return userpassword;
	}

	public void setUserpassword(String userpassword) {
		this.userpassword = userpassword;
	}

	public int getRole() {
		return role;
	}

	public void setRole(int role) {
		this.role = role;
	}

	@Override
	public String toString() {
		return "User [username=" + username + ", userpassword=" + userpassword + ", role=" + role + "]";
	}

}
